package stream.findany;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.testng.annotations.Test;


/**
 * findAny在非并行流和findFirst表现相同
 * 集合中没有元素，和集合包含null元素，对于findFirst来说是不同的，后者会产生NPE错误
 *
 * @author z
 */
public class FindAny {

    private final List<String> list = List.of("a", "c", "d", "i");

    @Test (invocationCount = 10)
    private void t1() {
        var res = list.stream()
            .findAny()
            .get();
        System.out.println(res);
    }

    @Test (invocationCount = 10)
    private void t2() {
        var list = new ArrayList<String>();
        list.add("g");
        list.add("l");
        list.add("k");
        list.add("q");
        var res = list.stream()
            .findAny()
            .get();
        System.out.println(res);
    }

    @Test (invocationCount = 10)
    private void t3() {
        var res = list.stream()
            .skip(new Random().nextInt(list.size()))
            .findAny()
            .get();

        System.out.println(res);
    }

    @Test
    private void t4() {

        var s = new ArrayList<>().stream()
            .findFirst();


        var list = new ArrayList<>();
        list.add(null);
//        List.of(null).stream()
//            .findFirst();
        list.stream()
            .findFirst();

        System.out.println(s);

    }

}
